package service

import (
	"fmt"
	"net"
)

func startUdpServer() {
	listen, err := net.ListenUDP("udp", &net.UDPAddr{
		IP:   net.IPv4(0, 0, 0, 0),
		Port: 30000,
	})
	if err != nil {
		fmt.Println("listen failed, err:", err)
		return
	}
	fmt.Println("111 ")
	defer listen.Close()
	var i int = 0
	for {
		var data [1024]byte
		//n, addr, err := listen.ReadFromUDP(data[:]) // 接收数据
		_, _, err := listen.ReadFromUDP(data[:]) // 接收数据
		//fmt.Println("30000")
		if err != nil {
			fmt.Println("read udp failed, err:", err)
			continue
		}
		i = i + 1
		if i%500000 == 0 {
			fmt.Println(i)
		}

		//fmt.Printf("data:%v addr:%v count:%v\n", string(data[:n]), addr, n)
		//_, err = listen.WriteToUDP(data[:n], addr) // 发送数据
		//if err != nil {
		//	fmt.Println("write to udp failed, err:", err)
		//	continue
		//}

	}
	fmt.Println("already ")
}

func startUdpClient() {
	socket, err := net.DialUDP("udp", nil, &net.UDPAddr{
		IP:   net.IPv4(192, 168, 0, 129),
		Port: 30000,
	})
	if err != nil {
		fmt.Println("1111,err:", err)
		return
	}
	defer socket.Close()
	var i int = 0
	for {
		sendData := []byte("Hello server")
		_, err = socket.Write(sendData)
		i = i + 1
		if i%100000 == 0 {
			fmt.Println(i)
		}
		if err != nil {
			fmt.Println("222,err:", err)
			return
		}
	}
	//data := make([]byte, 4096)
	//n, remoteAddr, err := socket.ReadFromUDP(data)
	//if err != nil {
	//	fmt.Println("33,err:", err)
	//	return
	//}
	//fmt.Printf("recv:%v addr:%v count:%v\n", string(data[:n]), remoteAddr, n)

}
